var DailyBalanceList = {};
DailyBalanceList.contextpath = "";
DailyBalanceList.initPage = function(contextpath) {
	DailyBalanceList.contextpath = contextpath;

	layui.use(['table','form', 'laydate'], function() {
		 var table = layui.table
		  ,layer = layui.layer
		  ,laydate = layui.laydate
		  ,form = layui.form;
		//日期
		  laydate.render({
		    elem: '#date',
		    done: function(value, date, endDate){
		    	var payType= $("#payType").val();
				var monthType= $("#monthType").val();
				var yearType= $("#yearType").val();
				var balanceType= $("#balanceType").val();
				var date= value;
				DailyBalanceList.reloadTable(table,laydate,payType,balanceType,date,yearType,monthType);
				
		    	}
		  });
		// 第一个实例
		table.render({
			elem : '#daily_balance_list_table',
			toolbar : '#toolbarDemo'
			,
			url : '/web2019/finance/ajaxQueryDailyBalance.action' // 数据接口
			,
			page : true // 开启分页
			, 
			where: 
			{
             payType :$("#payType").val(),
             monthType :$("#monthType").val(),
             yearType :$("#yearType").val(),
             balanceType :$("#balanceType").val(),
             date :$("#date").val()
            },
			response : {
				statusName : 'code' // 规定数据状态的字段名称，默认：code
				,
				statusCode : "0000" // 规定成功的状态码，默认：0
				,
				msgName : 'message' // 规定状态信息的字段名称，默认：msg
				,
				countName : 'total' // 规定数据总数的字段名称，默认：count
			},
			cols : [ [ // 表头
			 {
				field : 'balanceType',
				title : '分类',
				hide:  true,
				width : 100
			},{
				field : 'id',
				title : '编号',
				hide:  true,
				width : 100
			}, {
				field : 'balanceTypeName',
				title : '分类',
				width : 120,
				sort : true
			}, {
				field : 'payType',
				title : '支付类型',
				hide  : true,
				width : 120,
				sort  : true
			}, {
				field : 'payTypeName',
				title : '支付类型名称',
				width : 120,
				sort  : true
			}, {
				field : 'accountA',
				title : '账户A',
				hide:  true,
				width : 120,
				sort  : true
			}, {
				field : 'accountAName',
				title : '账户A',
				width : 120
			}, {
				field : 'accountB',
				title : '账户B',
				hide  : true,
				width : 120
			}, {
				field : 'accountBName',
				title : '账户B',
				width : 120
			}, {
				field : 'money',
				title : '金额',
				width : 120,
				templet: function(d){
				    	   return d.money+"元"
			    }
			}, {
				field : 'date',
				title : '日期',
				width : 120,
				sort : true
			}, {
				field : 'remark',
				title : '备注',
				width : 200,
				sort : true
			}
			, {
				title : '操作',
				templet : DailyBalanceList.setOperate,
				width : 160,
				align : 'center',
				fixed : 'right'
			}
			] ]
		});
		form.on('select(selectChange)', function(data){ 
			var payType= $("#payType").val();
			var monthType= $("#monthType").val();
			var yearType= $("#yearType").val();
			var balanceType= $("#balanceType").val();
			 $("#date").val("");
			DailyBalanceList.reloadTable(table,laydate,payType,balanceType,"",yearType,monthType);
		});
		
		  //监听工具条
		  table.on('tool(daily_balance_list_table_filter)', function(obj){
		    var data = obj.data;
		    if(obj.event === 'del'){
		      layer.confirm('真的删除行么', function(index){
		    	  DailyBalanceList.ajaxDeleteBalance(data);
		      });
		    } else if(obj.event === 'edit'){
		        layer.open({
		            type: 1
		            ,title: '修改任务'
		            ,area: ['590px', '460px']
		            ,shade: 0
		            ,maxmin: true
		            ,content: DailyBalanceList.getUpdateHtml(data)
		            ,btn: ['修改', '取消'] //只是为了演示
		            ,yes: function(){
		            	DailyBalanceList.ajaxUpdateBalance();
		            }
		            ,btn2: function(){
		              layer.closeAll();
		            }
		            ,success: function(layero){
		             
		            }
		          });
		    }
		  });
	});
	
}
DailyBalanceList.setOperate=function(data){
	var btnHtml="";
//	btnHtml += '<button type="button" class="layui-btn layui-btn-xs seeDetail" lay-event="edit">修改</button>';
	btnHtml += '<button type="button" class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>';
	return btnHtml;
}
DailyBalanceList.getUpdateHtml= function(obj){
	if(obj.payType=="1"){
		return DailyBalanceList.getUpdateHtmlPay(obj);
	}else if(obj.payType=="2"){
		return DailyBalanceList.getUpdateHtmlIncome(obj);
	}else if(obj.payType=="3"){
		return DailyBalanceList.getUpdateHtmlTransfer(obj);
	}
}


DailyBalanceList.getUpdateHtmlIncome= function(obj){
	var html="";
	html+='<input type="hidden" id="id" value="'+obj.id+'"/>';
	html+='<fieldset class="layui-elem-field layui-field-title"';
	html+='	style="margin-top: 20px;">';
	html+='<legend style="font-weight:200px;font-size:16px;">更新开支编号:'+obj.id+'</legend>';
	html+='</fieldset>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">金额</label>';
	html+='<div class="layui-input-block">';
	html+='	<input type="text" name="moneyNew" lay-verify="money" value="'+obj.money+'" style="width:300px;"';
	html+='	autocomplete="off" placeholder="请输入金额" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">备注</label>';
	html+='<div class="layui-input-block">';
	if(obj.description==null){
		html+='<textarea id="descriptionNew" name="remarkNew" style="width:400px;" placeholder="请输入备注" class="layui-textarea"></textarea>';	
	}else{
		html+='<textarea id="descriptionNew" name="remarkNew" style="width:400px;" placeholder="请输入备注" class="layui-textarea">'+obj.remark+'</textarea>';	
	}
	html+='</div>';
	html+='</div>';
	return html;
}

DailyBalanceList.getUpdateHtmlPay= function(obj){
	var html="";
	html+='<input type="hidden" id="id" value="'+obj.id+'"/>';
	html+='<fieldset class="layui-elem-field layui-field-title"';
	html+='	style="margin-top: 20px;">';
	html+='<legend style="font-weight:200px;font-size:16px;">更新开支编号:'+obj.id+'</legend>';
	html+='</fieldset>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">金额</label>';
	html+='<div class="layui-input-block">';
	html+='	<input type="text" name="moneyNew" lay-verify="money" value="'+obj.money+'" style="width:300px;"';
	html+='	autocomplete="off" placeholder="请输入金额" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">备注</label>';
	html+='<div class="layui-input-block">';
	if(obj.description==null){
		html+='<textarea id="descriptionNew" name="remarkNew" style="width:400px;" placeholder="请输入备注" class="layui-textarea"></textarea>';	
	}else{
		html+='<textarea id="descriptionNew" name="remarkNew" style="width:400px;" placeholder="请输入备注" class="layui-textarea">'+obj.remark+'</textarea>';	
	}
	html+='</div>';
	html+='</div>';
	return html;
}

DailyBalanceList.getUpdateHtmlTransfer= function(obj){
	var html="";
	html+='<input type="hidden" id="id" value="'+obj.id+'"/>';
	html+='<fieldset class="layui-elem-field layui-field-title"';
	html+='	style="margin-top: 20px;">';
	html+='<legend style="font-weight:200px;font-size:16px;">更新开支编号:'+obj.id+'</legend>';
	html+='</fieldset>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">金额</label>';
	html+='<div class="layui-input-block">';
	html+='	<input type="text" name="moneyNew" lay-verify="money" value="'+obj.money+'" style="width:300px;"';
	html+='	autocomplete="off" placeholder="请输入金额" class="layui-input">';
	html+='</div>';
	html+='</div>';
	html+='<div class="layui-form-item">';
	html+='<label class="layui-form-label">备注</label>';
	html+='<div class="layui-input-block">';
	if(obj.description==null){
		html+='<textarea id="descriptionNew" name="remarkNew" style="width:400px;" placeholder="请输入备注" class="layui-textarea"></textarea>';	
	}else{
		html+='<textarea id="descriptionNew" name="remarkNew" style="width:400px;" placeholder="请输入备注" class="layui-textarea">'+obj.remark+'</textarea>';	
	}
	html+='</div>';
	html+='</div>';
	return html;
}

//重新加载table
DailyBalanceList.reloadTable= function(table,laydate,payType,balanceType,date,yearType,monthType){
	table.reload('daily_balance_list_table', {
        page: {
            curr: 1
        },
		where: 
		{
		 payType :payType,
		 yearType :yearType,
		 monthType :monthType,
		 balanceType:balanceType,
		 date:date
	    }
    });
	$("#payType").val(payType);
	$("#monthType").val(monthType);
	$("#yearType").val(yearType);
	$("#balanceType").val(balanceType);
	$("#date").val(date);

	layui.form.render('select');
	laydate.render({
		  elem: '#date',
		  done: function(value, date, endDate){
			  	var payType= $("#payType").val();
				var monthType= $("#monthType").val();
				var yearType= $("#yearType").val();
				var balanceType= $("#balanceType").val();
				var date= value;
				DailyBalanceList.reloadTable(table,laydate,payType,balanceType,date,yearType,monthType);
		  }
	});
}

DailyBalanceList.ajaxDeleteBalance = function(data1) {
	$.ajax({
		type : "POST",
		url : "/web2019/finance/ajaxDeleteBalance.action",
		dataType : "json",
		data : data1,
		success : function(data) {
			if (data.code == "0000") {
				layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
					layer.closeAll();
					layui.use(['table'], function() {
						var payType= $("#payType").val();
						var monthType= $("#monthType").val();
						var yearType= $("#yearType").val();
						var balanceType= $("#balanceType").val();
						var date= $("#date").val();
						var table = layui.table;
						var laydate =layui.laydate;
						DailyBalanceList.reloadTable(table,laydate,payType,balanceType,date,yearType,monthType);
					 });
				});
			} else {
				
			}
		},
		error : function(data) {

		}
	});
}



DailyBalanceList.ajaxUpdateTask =function(){
	var taskId =$("#taskId").val();
	var taskName =$("input[name='taskNameNew']").val(); 
	var state =$("#stateNew").val();
	var time =$("input[name='timeNew']").val();
	var description =$("#descriptionNew").val();
	$.ajax({
		type : "POST",
		url : "/web2019/project/ajaxUpdateTask.action",
		dataType : "json",
		data : {"taskId":taskId,"taskName":taskName,"state":state,"time":time,"description":description},
		success : function(data) {
			if (data.code == "0000") {
				layer.alert(data.message, { icon: 1, closeBtn: 0 }, function (index) { 
					layer.closeAll();
					layui.use(['table'], function() {
						var projectId= $("#projectId").val();
						var taskType= $("#taskType").val();
						var taskState= $("#taskState").val();
						var table = layui.table;
						DailyBalanceList.reloadTable(table,projectId,taskType,taskState);
						$("#projectId").val(projectId);
						$("#taskType").val(taskType);
						$("#taskState").val(taskState);
						layui.form.render('select');
					 });
				});
			} else {
				
			}
		},
		error : function(data) {
		}
	});
}
